package cn.com.hhrcw.service.impl;

import cn.com.hhrcw.entity.*;
import cn.com.hhrcw.mapper.*;
import cn.com.hhrcw.service.IEnInfoService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.enums.Roles;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 公司信息表 服务实现类
 * </p>
 *
 * @author jiangdong
 * @since 2020-10-29
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class EnInfoServiceImpl extends ServiceImpl<EnInfoMapper, EnInfo> implements IEnInfoService {

    @Autowired
    private MySysUserMapper sysUserMapper;

    @Autowired
    private PiInfoMapper piInfoMapper;

    @Autowired
    private OpJobMapper opJobMapper;

    @Autowired
    private OpDeliveryHistoryMapper opDeliveryHistoryMapper;

    @Autowired
    private MySysUserDepartMapper sysUserDepartMapper;

    @Autowired
    private ISysBaseAPI sysBaseAPI;

    @Override
    public IPage<EnInfo> page(IPage<EnInfo> page, Wrapper<EnInfo> queryWrapper) {
        return this.baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public Integer getFeedbackCount(Map<String, String> map) {
        return baseMapper.getFeedbackCount(map);
    }

    /**
     * 创建企业
     *
     * @param user
     * @param piInfo
     * @param enInfo
     * @return
     */
    @Override
    public boolean createCompany(SysUser user, PiInfo piInfo, EnInfo enInfo) {
        sysUserMapper.insert(user);
        piInfo.setUserId(user.getId());
        piInfoMapper.insert(piInfo);
        this.save(enInfo);
        return true;
    }

    @Override
    public boolean deleteCompany(String[] id) {
        List<EnInfo> list = this.lambdaQuery().in(EnInfo::getId, id).list();
        list = list.stream().map(e -> e.setDelFlag(true)).collect(Collectors.toList());
        for (EnInfo enInfo : list) {
            this.updateById(enInfo);
        }
        //删除企业招聘信息
        LambdaQueryWrapper<OpJob> opJobLambdaQueryWrapper = Wrappers.<OpJob>lambdaQuery().in(OpJob::getCompanyId, id);
        List<OpJob> opJobs = this.opJobMapper.selectList(opJobLambdaQueryWrapper);
        for (OpJob opJob : opJobs) {
            opJob.setDelFlag(true);
            this.opJobMapper.updateById(opJob);
        }
        //删除职位投递信息
        LambdaQueryWrapper<OpDeliveryHistory> opDeliveryHistoryLambdaQueryWrapper = Wrappers.<OpDeliveryHistory>lambdaQuery().in(OpDeliveryHistory::getCompanyId, id);
        List<OpDeliveryHistory> opDeliveryHistories = opDeliveryHistoryMapper.selectList(opDeliveryHistoryLambdaQueryWrapper);
        for (OpDeliveryHistory opDeliveryHistory : opDeliveryHistories) {
            opDeliveryHistory.setDelFlag(true);
            this.opDeliveryHistoryMapper.updateById(opDeliveryHistory);
        }
        //删除用户与公司关系
        LambdaQueryWrapper<SysUserDepart> sysUserDepartLambdaQueryWrapper = Wrappers.<SysUserDepart>lambdaQuery().in(SysUserDepart::getDepId, id);
        List<SysUserDepart> userDeparts = this.sysUserDepartMapper.selectList(sysUserDepartLambdaQueryWrapper);
        for (SysUserDepart userDepart : userDeparts) {
            LambdaQueryWrapper<PiInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(PiInfo::getUserId, userDepart.getUserId());
            PiInfo piInfo = piInfoMapper.selectOne(lambdaQueryWrapper);
            if (piInfo != null) {
                piInfo.setEnRoleType(0);
                piInfo.setDelFlag(true);
                piInfo.setPhone(piInfo.getUserId() + "@" + piInfo.getPhone());
                piInfo.setEmail(piInfo.getUserId() + "@" + piInfo.getEmail());
                piInfo.setUsername(piInfo.getUserId() + "@" + piInfo.getUsername());

                SysUser sysUser = sysUserMapper.selectById(piInfo.getUserId());

                sysUser.setEmail(sysUser.getId() + "@" + sysUser.getEmail());
                sysUser.setUsername(sysUser.getId() + "@" + sysUser.getUsername());
                sysUser.setDelFlag(true);
                sysUser.setPhone(sysUser.getId() + "@" + sysUser.getPhone());
                sysUserMapper.updateById(sysUser);
                piInfoMapper.updateById(piInfo);
                sysBaseAPI.removeRole(userDepart.getUserId(), Roles.ENT);
            }
            this.sysUserDepartMapper.deleteById(userDepart.getId());
        }
        return true;
    }
}
